#
# test_global.rb - global application correctness test
#

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'Automaton'
require 'Optimizer'
require 'Matcher'

class TestGlobal < Test::Unit::TestCase
  def test_from_file
    
    #Checking file existance to avoid errors while opening it
    if !File.exists?("testlist.txt")
      puts "ERROR: File not found"
      exit(1)
    end
    
    testfile = File.open("testlist.txt", "r")
    
    prev = ""
    num = 0
    
    testfile.each_line do |line|
      
      current = []
      current = line.chomp.split('@')
    
      #Not considering lines starting with '#' character, so, you we can add comment on the file
      test = []
      test = current[0].split
      test = test[0]
    
      if test != '#'
        #Checking if we can reuse past automaton (ie if we have the same pattern)
        #if current[0] != prev
          automa = Automaton.new(current[0])
          first_state = automa.espressione()
          ic = automa.ic
          state1 = automa.state1
          state2 = automa.state2
          ottimizzazione = Optimizer.new(ic, state1, state2, first_state)
          ottimizzazione.ottimizza
          first_state = ottimizzazione.first_state
          ic = ottimizzazione.ic
          state1 = ottimizzazione.state1
          state2 = ottimizzazione.state2
        #end
    
        prev = current[0]
      
        matcher = Matcher.new(ic, state1, state2, first_state)
        num_match = matcher.ricerca_scan(current[1])
      
        if num_match >= 1    
          check = true
        end
    
        num += 1
        
        if current[2] == 'true'
          boolean = true
        else 
          boolean = false
        end
      
        assert_equal(check, boolean, "While performing global test, on " + num.to_s + " ° line" )
    
      end
    end
    
  end
end
